package com.fang.mmy.items.task;

import com.fang.mmy.items.constants.Constants;
import com.fang.mmy.items.service.lmpl.ItemServicelmpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.*;

/**
 * @ClassName ItemListTask
 * @Author kebukeyi
 * @Date 2022/8/12 16:10
 * @Description
 * @Version 1.0.0
 */
public class ItemListTask {

    public static LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<>();

    private static ExecutorService preheatingItemService = creatExecutors();

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ItemServicelmpl itemServicelmpl;

    private static ExecutorService creatExecutors() {
        return new ThreadPoolExecutor(5, 5, 60L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1024));
    }


    // 用户登录后 并行预热 任务列表
    public void preheatingItem() {
        preheatingItemService.submit(() -> {
            String userId = linkedBlockingQueue.poll();
            redisTemplate.opsForSet().add(Constants.LoginUserId, (userId));
            itemServicelmpl.displayList(100001L, Long.getLong(userId));
        });
    }




    public static void main(String[] args) {
        ItemListTask itemListTask = new ItemListTask();
        itemListTask.preheatingItem();
    }

}
